package com.second.app.service.impl;

import com.second.app.entity.Example;
import com.second.app.mapper.ExampleMapper;
import com.second.app.service.IExampleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 1542365144@qq.com
 * @since 2024-04-20
 */
@Service
public class ExampleServiceImpl extends ServiceImpl<ExampleMapper, Example> implements IExampleService {

    @Autowired
    private ExampleMapper exampleMapper;

    @Transactional
    @Override
    public void updateExampleWithManualLock(Long id, String data) {

        Example example = exampleMapper.selectForUpdateById(id);

        System.out.println(Thread.currentThread().getName() + ": 锁住这一行，查询得到data的值是：" + example.getData()+"要更新的值为："+data);

        example.setData(data);

        System.out.println("阻塞5s！");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        exampleMapper.updateById(example);

        System.out.println("释放事务！");
    }
}
